//
//  ZFDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "ZFDetViewController.h"

#import "HouseDetView.h"
#import "HouseTSCell.h"
#import "PayForViewController.h"
@interface ZFDetViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSMutableDictionary *dic;
    NSMutableArray *array;
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,copy)NSString *str;//订单号

@property (nonatomic,strong)NSMutableArray *imageArrays;//图片数组

@end

@implementation ZFDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBGColor];
    [self showNav:@"详情"];
    dic = [NSMutableDictionary dictionary];
    _imageArrays = [NSMutableArray array];
    
    [self loadData];
    [self loadNum];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-50) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    //底部视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-50, ScreenWidth, 50)];
    view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self.view addSubview:view];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth/3*2, 0, ScreenWidth/3, 50.0);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"联系" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    UIView *viewH = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50.0, 50.0)];
    viewH.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [view addSubview:viewH];
    
    UIImageView *image = [[UIImageView alloc] init];
    image.image = [UIImage imageNamed:@"电话"];
    [view addSubview:image];
    [image makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(viewH.centerX);
        make.centerY.equalTo(viewH.centerY);
    }];
    
    UIView *viewT = [[UIView alloc] init];
    viewT.backgroundColor = [UIColor whiteColor];
    [view addSubview:viewT];
    [viewT makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewH.right);
        make.right.equalTo(btn.left);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(50);
    }];
    
    UILabel *labPhone = [[UILabel alloc] init];
    NSString *phone = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"phone"] integerValue]];
    NSString *str1 = [phone substringToIndex:3];
    NSString *str2 = [phone substringFromIndex:9];
    labPhone.text = [NSString stringWithFormat:@"%@****%@",str1,str2];
    
    
    labPhone.textColor = [UIColor darkGrayColor];
    labPhone.font = [UIFont systemFontOfSize:14.0];
    [viewT addSubview:labPhone];
    [labPhone makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewT.left).offset(8);
        make.centerY.equalTo(viewT.centerY).offset(0);
    }];
    
    HouseDetView *headview = [[[NSBundle mainBundle] loadNibNamed:@"HouseDetView" owner:nil options:nil] lastObject];
    headview.frame = CGRectMake(0, 0, ScreenWidth, 630);
    _tableView.tableHeaderView = headview;
    //
    UIView *v = [WTImageScroll ShowNetWorkImageScrollWithFream:CGRectMake(0, 0, ScreenWidth, 264) andImageArray:_imageArrays andBtnClick:^(NSInteger tagValue) {
        
    }];
    headview.images.userInteractionEnabled = YES;
    [headview.images addSubview:v];
    
    
    headview.labTit.text = [dic objectForKey:@"title"];
    headview.labPrice.text = [NSString stringWithFormat:@"%@元/月",[dic objectForKey:@"mnthly"]];
    headview.labPayState.text = [NSString stringWithFormat:@"(%@)",[dic objectForKey:@"payment"]];
    headview.labHX.text = [dic objectForKey:@"room"];
    headview.labRentState.text = [dic objectForKey:@"mode"];
    headview.labArea.text = [NSString stringWithFormat:@"%@㎡",[dic objectForKey:@"area"]];
    //房源特色
    headview.labZX.text = [dic objectForKey:@"rnovation"];
    headview.labLC.text = [NSString stringWithFormat:@"%@层",[dic objectForKey:@"floor"]];
    headview.labHouseStyle.text = [dic objectForKey:@"type"];
    headview.labDate.text = [dic objectForKey:@"reasetime"];
    headview.labPlace.text = [dic objectForKey:@"community"];
    headview.labCX.text = [dic objectForKey:@"toward"];
    headview.labYT.text = [dic objectForKey:@"use"];
    headview.labYear.text = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"bilttime"] integerValue]];
    headview.labKM.text = [NSString stringWithFormat:@"%.2fkm",[[dic objectForKey:@"km"] floatValue]];
    
}
//tel 租房订单支付
- (void)click{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    NSString *houseid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    [parmas setObject:houseid forKey:@"rid"];
    [parmas setObject:@"1" forKey:@"cost"];//金额
    [parmas setObject:@"2" forKey:@"payment"];
    [parmas setObject:[dic objectForKey:@"title"] forKey:@"rentname"];
    [parmas setObject:_str forKey:@"ordernumber"];
    
    NSString *ischarge = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"ischarge"] integerValue]];//2收费3不收费
    if ([ischarge integerValue] == 2) {//2收费
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"为了防止盗取房东信息，以及平台维护费用，现收取1元手续费，支付后可查看联系方式，谢谢支持！" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            PayForViewController *pay = [[PayForViewController alloc] init];
            pay.dic = parmas;
            pay.OrderName = @"查看房源联系方式";
            pay.price = @"1";
            [self.navigationController pushViewController:pay animated:YES];
        }];
        
        [alert addAction:action1];
        [alert addAction:action2];
        
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        NSString *phone = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"phone"] integerValue]];
        NSLog(@"phone:%@",phone);
        NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
        UIWebView *web = [[UIWebView alloc] init];
        [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:web];
    }
    
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell2";
    HouseTSCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HouseTSCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *op = array[indexPath.row];
    
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[op objectForKey:@"picimga"]];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"timg-(2)"]];
    cell.labTit.text = [op objectForKey:@"instructiona"];
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 330.0;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, ScreenWidth-32, 100)];
    lab2.numberOfLines = 0;
    lab2.text = [dic objectForKey:@"text"];
    lab2.font = [UIFont systemFontOfSize:13.0];
    [view addSubview:lab2];
    
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (array.count<=0) {
        return 0;
    }
    return 100;
}

//租房详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_lng forKey:@"longitude"];
    [parmas setObject:_lat forKey:@"latitude"];
    [parmas setObject:_hID forKey:@"rentid"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    NSLog(@"parmas:%@",parmas);
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Rentout/rentdetails",urlHead];
    [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"租房详情res:%@",responseObject);
        dic = [responseObject objectForKey:@"result"];
        NSString *pic = [dic objectForKey:@"pic"];
        NSArray *arr = [pic componentsSeparatedByString:@"|"];
        for (NSString *aaa in arr) {
            NSString *imageUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],aaa];
            [_imageArrays addObject:imageUrl];
        }
        
        array = [dic objectForKey:@"teristic"];
        
        [self loadUI];
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

//生成订单号
- (void)loadNum{
    NSString *url = [NSString stringWithFormat:@"%@/App/Shoppingcart/ordernumber",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        _str = [responseObject objectForKey:@"result"];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
